<?php 
/**
 *	数据验证类
 *
 * @copyright (c) 2006 - 2007 与非网 (www.eefocus.com)
 * @author	kokko<kokko313@gmail.com>
 * @package com
 * @subpackage eefocus
 * @version 1.0 2007-11-7
 */

class Validation
{//类定义开始

    /**
     +----------------------------------------------------------
     * 预定义验证格式
     +----------------------------------------------------------
     * @var integer
     * @access protected
     +----------------------------------------------------------
     */
   static protected $_regex = array(
            'require'=> '/.+/',
            'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
            'phone' => '/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/',
            'mobile' => '/^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/',
            'url' => '/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/',
            'currency' => '/^\d+(\.\d+)?$/',
            'number' => '/^\d+$/',
            'zip' => '/^[1-9]\d{5}$/',
            'qq' => '/^[1-9]\d{4,8}$/',
            'integer' => '/^[-\+]?\d+$/',
            'double' => '/^[-\+]?\d+(\.\d+)?$/',
            'english' => '/^[A-Za-z]+$/',
            'chinese' =>  '/^[\u0391-\uFFE5]+$/',
            'username' => '/^[a-z0-9]\w{4,15}$/i',
            'password' => '/^[a-z0-9]\w{4,15}$/i',
            'unsafe' => '/^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/'
    );

    /**
     +----------------------------------------------------------
     * 架构函数
     * 
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
    function __construct()
    {

    }

    /**
     * 验证数据项
     * 
     * @access public 
	 * @static
     * @param string $checkName 验证的数据类型名或正则式
     * @param string $value  要验证的数据
     * @return boolean
     */
    static function check($value,$checkName) 
    {
        $matchRegex = self::getRegex($checkName);
        return preg_match($matchRegex,trim($value));
    }


    /**
     * 取得验证类型的正则表达式
     * 
     * @access public
	 * @static
     * @param string $name 验证类型名称
     * @return string
     */
    static function getRegex($name) 
    {
        if(isset(self::$_regex[strtolower($name)])) {
            return self::$_regex[strtolower($name)];
        }else {
        	return $name;
        }
    }
}//类定义结束
?>